package cn.datawin.bms.modules.customer.controller;

import cn.datawin.bms.customer.entity.CstmCorporateEntity;
import cn.datawin.bms.modules.customer.service.CstmCorporateService;
import cn.datawin.common.core.util.R;
import cn.datawin.common.log.annotation.SysLog;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 下游客户公司
 *
 * @author datawin-sms
 * @date 2025-06-16 21:00:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/customer/corporate" )
@Tag(description = "cstmCorporate" , name = "下游客户公司管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CstmCorporateController {

    private final CstmCorporateService cstmCorporateService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param cstmCorporate 下游客户公司
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('customer_corporate_view')" )
    public R getCstmCorporatePage(@ParameterObject Page page, @ParameterObject CstmCorporateEntity cstmCorporate) {
        LambdaQueryWrapper<CstmCorporateEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(cstmCorporate.getName()),CstmCorporateEntity::getName,cstmCorporate.getName());
		wrapper.eq(StrUtil.isNotBlank(cstmCorporate.getCode()),CstmCorporateEntity::getCode,cstmCorporate.getCode());
        return R.ok(cstmCorporateService.page(page, wrapper));
    }


	/**
	 * 获取所有下游公司
	 */
	@Operation(summary = "获取所有下游公司" , description = "获取所有下游公司" )
	@GetMapping("/listAll" )
	@PreAuthorize("isAuthenticated()")  // 只需要登录权限
	public R getAllCstmCorporate() {
		LambdaQueryWrapper<CstmCorporateEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(CstmCorporateEntity::getStatus,1);
		return R.ok(cstmCorporateService.list(wrapper));
	}


    /**
     * 通过id查询下游客户公司
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('customer_corporate_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(cstmCorporateService.getById(id));
    }

    /**
     * 新增下游客户公司
     * @param cstmCorporate 下游客户公司
     * @return R
     */
    @Operation(summary = "新增下游客户公司" , description = "新增下游客户公司" )
    @SysLog("新增下游客户公司" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('customer_corporate_add')" )
    public R save(@RequestBody CstmCorporateEntity cstmCorporate) {
        return R.ok(cstmCorporateService.save(cstmCorporate));
    }

    /**
     * 构建新下游客户公司
     * @return R
     */
    @Operation(summary = "构建新下游客户公司" , description = "构建新下游客户公司" )
    @SysLog("构建新下游客户公司" )
    @GetMapping("/new")
    @PreAuthorize("@pms.hasPermission('customer_corporate_add')" )
    public R newCstmCorporate() {
        return R.ok(cstmCorporateService.newCstmCorporate());
    }

    /**
     * 修改下游客户公司
     * @param cstmCorporate 下游客户公司
     * @return R
     */
    @Operation(summary = "修改下游客户公司" , description = "修改下游客户公司" )
    @SysLog("修改下游客户公司" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('customer_corporate_edit')" )
    public R updateById(@RequestBody CstmCorporateEntity cstmCorporate) {
        return R.ok(cstmCorporateService.updateById(cstmCorporate));
    }

    /**
     * 通过id删除下游客户公司
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除下游客户公司" , description = "通过id删除下游客户公司" )
    @SysLog("通过id删除下游客户公司" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('customer_corporate_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(cstmCorporateService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param cstmCorporate 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('customer_corporate_export')" )
    public List<CstmCorporateEntity> export(CstmCorporateEntity cstmCorporate,Long[] ids) {
        return cstmCorporateService.list(Wrappers.lambdaQuery(cstmCorporate).in(ArrayUtil.isNotEmpty(ids), CstmCorporateEntity::getId, ids));
    }
}
